# -*- coding: utf-8 -*-
import json
import os
from time import sleep

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QSize, QObject
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QDockWidget, QAction, QFileDialog
import cv2.cv2 as cv2
import numpy as np
import copy


class ImageManager(QObject):
    update_signal = QtCore.pyqtSignal(QtGui.QImage)

    def __init__(self):
        super().__init__()
        self._curImgMat = None
        self.originMat = None

    @property
    def img(self):
        return self._curImgMat

    @img.setter
    def img(self, value):
        self._curImgMat = value

    @img.deleter
    def img(self):
        self._curImgMat = np.zeros()

    def setOriginMat(self, src_mat):
        self._curImgMat = src_mat
        self.originMat = copy.deepcopy(src_mat)

    def getOriginMat(self):
        return self.originMat

    def showImg(self, name, img):
        cv2.namedWindow(name,cv2.WINDOW_AUTOSIZE)
        cv2.imshow(name, img)
        cv2.waitKey(0)

    def saveImg(self, name, img):
        cv2.imwrite(name, img)

    def dispImg(self, imgMat):
        if len(imgMat.shape) == 3:
            img = cv2.cvtColor(imgMat, cv2.COLOR_BGR2RGB)
        else:
            img = cv2.cvtColor(imgMat, cv2.COLOR_GRAY2RGB)
        h, w = img.shape[:2]
        h = (h + 3) // 4
        w = (w + 3) // 4
        shrink = cv2.resize(img, (4 * w, 4 * h), interpolation=cv2.INTER_AREA)
        QtImg = QtGui.QImage(shrink.data,
                             shrink.shape[1],
                             shrink.shape[0],
                             QtGui.QImage.Format_RGB888)
        self.update_signal.emit(QtImg)


img_mng = ImageManager()
